package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.job.JobAddReqDTO;
import com.yixin.hospital.entity.dto.req.job.JobEditReqDTO;
import com.yixin.hospital.service.JobService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/job")
@Api(value = "职业管理", tags = "职业管理")
public class JobController {

    @Resource
    private JobService jobService;

    @GetMapping("/list")
    @ApiOperation(value = "职业列表")
    public ApiResult list() {
        return ApiResult.success(jobService.list());
    }

    @GetMapping("/treeList")
    @ApiOperation(value = "职业列表树")
    public ApiResult treeList() {
        return jobService.treeList();
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加职业")
    public ApiResult add(@Valid @RequestBody JobAddReqDTO jobAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobService.add(jobAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑职业")
    public ApiResult edit(@Valid @RequestBody JobEditReqDTO jobEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobService.edit(jobEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除职业")
    public ApiResult remove(@PathVariable int id) {
        jobService.removeById(id);
        return ApiResult.success();
    }
}
